﻿using _51_AOP_DAL;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;

namespace _52_鉴权授权.Utility
{
    public class QQHandler : AuthorizationHandler<QQEmailRequirment>
    {
        private readonly IUserService _userService;
        public QQHandler(IUserService userService) { 
            this._userService= userService;
        }
        
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, QQEmailRequirment requirement)
        {
            Console.WriteLine("授权策略，数据拿去和数据库做对比");
            string userid = context.User.Claims.First(c => c.Type == "Userid").Value;
            string email = context.User.Claims.First(c => c.Type == ClaimTypes.Email).Value;
            Console.WriteLine("email=" + email);
            _userService.Validata(email);
            // 在这里做验证
            if (true)
            {
                context.Succeed(requirement);
            }
            return Task.CompletedTask;
        }
    }
}
